package rua.jose.macamo;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.WindowManager;

public class SpotActivity extends Activity {
   protected SpotView game;

   @Override
   public void onCreate(final Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //Force landscape orientation
      //Enable full screen:
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
      final Display  d = getWindowManager().getDefaultDisplay(); //Get the screen dimensions
      final Bundle b = getIntent().getExtras(); //Get the bundle passed by the launching activity
      final String filename = b.getString("xml"); //Get the XML-filename
      game = new SpotView(this, d.getWidth(), d.getHeight(), filename); //Initialize the game view
      setContentView(game);
      takeKeyEvents(true);
   }

   @Override
   public boolean onKeyDown(final int keyCode, final KeyEvent msg) {
      if (keyCode == KeyEvent.KEYCODE_BACK) { //When the Back-key is pressed, close this Activity
         System.gc();
         finish();
      }
      return true;
   }
   @Override
   public boolean onKeyUp(final int keyCode, final KeyEvent msg) {
      return true;
   }

   @Override
   protected Dialog onCreateDialog(final int id) {
      AlertDialog.Builder dlgAlert;
      dlgAlert = new AlertDialog.Builder(this);
      if (id == 1) {
         dlgAlert.setTitle("Congratulations");
         dlgAlert.setMessage("You win!");
         dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog, final int which) {
               try {
                  game.kill();
               } catch (final InterruptedException ie)  {}
               game = null;
               System.gc();
               finish();
            }
         });
         dlgAlert.setCancelable(false);
      }
      return dlgAlert.create();
   }
}